<template>
	<view class="myUser" :style="{paddingTop: `${search.top+search.height+5}px`}">
		<image class="myUser_bg" src="/static/images/userTop.png"></image>
		<view class="myUser_MMain" :style="{paddingTop: `${search.top+search.height+5}px`}">
			<!-- 个人信息区 -->
			<button class="myUser__message" v-if="user&&!user.phone" open-type="getPhoneNumber"
				@getphonenumber="getPhoneNumber">
				<image  @click.stop="ff_previewImage(user.avatar)" class="myUser__messageAdmin" :src="user.avatar"></image>
				<view class="myUser__messageRight">
					<view class="myUser__messageRightBox">
						<p>{{user.nickname}}</p>
						<image class="myUser__messageRightBoxImg" src="/static/images/rightSign.png"></image>
					</view>
					<image mode="heightFix" class="myUser__messageRightBoxTitle"
						:src="'/static/images/titleIphone.png'">
					</image>
				</view>
			</button>
			<view class="myUser__message" v-else-if="user">
				<image @click.stop="ff_previewImage(user.avatar)" class="myUser__messageAdmin" :src="user.avatar"></image>
				<view class="myUser__messageRight">
					<view class="myUser__messageRightBox">
						<p>{{user.nickname}}</p>
						<!-- <image class="myUser__messageRightBoxImg" src="/static/images/rightSign.png"></image> -->
					</view>
					<image mode="heightFix" class="myUser__messageRightBoxTitle"
						:src="user.isPartner ? '/static/images/titleHur.png' :'/static/images/titleVip.png'">
					</image>
				</view>
			</view>
			<view class="myUser__message" v-else @click.stop="getUserInfo">
				<image class="myUser__messageAdmin" src="/static/images/xiaonian.png"></image>
				<view class="myUser__messageRight">
					<view class="myUser__messageRightBox">
						<p>点击登录</p>
						<image class="myUser__messageRightBoxImg" src="/static/images/rightSign.png"></image>
					</view>
					<view class="myUser__messageLeftssBox">登录福利惊喜多多</view>
				</view>
			</view>
			<!-- 钱包 -->
			<view v-if="user && isShowBox" class="myUser__wallet"
				@click.stop="goPath(`/package/myWallet/index?walletMoney=${user.walletMoney}&frozenMoney=${user.frozenMoney}`)">
				<view class="myUser__walletTop">
					<view class="myUser__walletTopMeText">我的钱包</view>
					<view class="myUser__walletTopRight">
						<view class="myUser__walletTopRightAll">查看全部</view>
						<image class="myUser__messageRightBoxImg" src="/static/images/rightSign.png"></image>
					</view>
				</view>
				<view class="myUser__walletBottom">
					<view class="myUser__walletBottomItem">
						<view class="myUser__walletBottomItemProe">{{user.walletMoney}}</view>
						<view class="myUser__walletBottomItemText">我的余额</view>
					</view>
					<view class="myUser__walletBottomItem">
						<view class="myUser__walletBottomItemProe">{{user.frozenMoney}}</view>
						<view class="myUser__walletBottomItemText">待入帐</view>
					</view>
				</view>
			</view>

			<!-- 订单 -->
			<view v-if="user" class="myUser__wallet dd" @click.stop="goPath(`/package/myOrder/index?index=0`)">
				<view class="myUser__walletTop">
					<view class="myUser__walletTopMeText">我的订单</view>
					<view class="myUser__walletTopRight">
						<view class="myUser__walletTopRightAll">查看全部</view>
						<image class="myUser__messageRightBoxImg" src="/static/images/rightSign.png"></image>
					</view>
				</view>
				<view class="myUser__walletBottomBox">
					<view class="myUser__walletBottomMain">
						<view v-if="index != 3" class="myUser__walletBottomMainItem"
							@click.stop="goPath(`/package/myOrder/index?index=${index+1}`)"
							v-for="(item,index) in formArr" :key="index">
							<view class="myUser__walletBottomMainItemTop">
								<image class="myUser__walletBottomMainItemTopImg" :src="item.path"></image>
								<view class="myUser__walletBottomMainItemTopNum" v-if="item.num != 0">{{item.num}}
								</view>
							</view>
							<view class="myUser__walletBottomMainItemBottomTxtx">{{item.text}}</view>
						</view>
						<view v-if="index == 3" class="myUser__walletBottomMainItem"
							@click.stop="goPath(`/package/myAftersale/index`)" v-for="(item,index) in formArr"
							:key="index">
							<view class="myUser__walletBottomMainItemTop">
								<image class="myUser__walletBottomMainItemTopImg" :src="item.path"></image>
								<view class="myUser__walletBottomMainItemTopNum" v-if="item.num != 0">{{item.num}}
								</view>
							</view>
							<view class="myUser__walletBottomMainItemBottomTxtx">{{item.text}}</view>
						</view>
					</view>
				</view>
			</view>

			<!-- 功能 -->
			<view v-if="user" class="myUser__walletff">
				<view class="myUser__walletTop marTop">
					<view class="myUser__walletTopMeText">其他功能</view>
				</view>
				<!-- user.isPartner -->

				<view class="myUser__walletBottomBigBox">
					<view v-for="(item,index) in formArrTwo" :key="index">
						<view @click="goPath(item.url,index)"
							v-if="(index==3&&user.isPartner==1)||(index==2&&user.isPartner==1)||(index==1&&user.isPartner==1&&user.referrerQrcode)||(index!=2&&index!=1&&index!=3)"
							class="myUser__walletBottomBigMainItem">
							<image class="myUser__walletBottomBigMainItemTopImg" :src="item.path"></image>
							<view class="myUser__walletBottomBigMainItemTopNum">{{item.text}}</view>
							<image class="myUser__messageRightBoxImg" src="/static/images/rightSignTwo.png"></image>
						</view>
					</view>
				</view>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		components: {

		},
		data() {
			return {
				isShowBox: false,
				user: null,
				formArr: [{
						path: '/static/images/unpaidIcon.png',
						text: '待支付',
						num: 2
					},
					{
						path: '/static/images/pendingIcon.png',
						text: '待发货',
						num: 2
					},
					{
						path: '/static/images/waitIcon.png',
						text: '待收货',
						num: 2
					},
					{
						path: '/static/images/afterIcon.png',
						text: '售后',
						num: 2
					}
				],
				formArrTwo: [{
						path: '/static/images/addressIcon.png',
						text: '收货地址',
						url: '/package/profile/index'
					},
					{
						path: '/static/images/codeIcon.png',
						text: '我的二维码',
						url: '/package/myCode/index'
					},
					{
						path: '/static/images/recommendationIcon.png',
						text: '我的推荐',
						url: '/package/myRecommend/index'
					},
					{
						path: '/static/images/performanceIcon.png',
						text: '我的业绩',
						url: '/package/myPerformance/index'
					},
					{
						path: '/static/images/pbdIcon.png',
						text: '关于拼表多',
						url: '/package/myRegards/index'
					}
				]
			}
		},
		onLoad() {
			// #ifndef H5
			let search = uni.getMenuButtonBoundingClientRect()
			this.$store.commit('updataSearch', search);
			// #endif

		},
		onShow() {
			this.$api.wxGetOtherSetInfo().then(res=>{
				if(res.code==200){
					this.isShowBox = res.data.pageShow == this.versions ? true : false;
					uni.setStorageSync('servicePhone',res.data.servicePhone);
				}
			})
			this.initUser();
		

		},
		computed: {
			search() {
				return this.$store.state.search;
			},
			versions() {
				return this.$store.state.versions;
			},

		},
		methods: {
			getPhoneNumber(e) {
				console.log(e)
				let req = {
					iv: e.detail.iv,
					encryptedData: e.detail.encryptedData
				}
				uni.showLoading({
					title: '加载中',
					mask: true
				});
				this.$api.userWxBindPhone(req).then(res => {
					if (res.code == 200) {
						uni.showToast({
							icon: 'none',
							title: '手机绑定成功',
							mask: true
						});
						this.initUser();
					}
				})
			},
			getUserInfo() {
				let that = this;
				let partnerNo = uni.getStorageSync('partnerNo')
				this.initUserx().then(user => {

					if (user == null) {

						uni.showModal({
							title: '温馨提示',
							content: '亲，授权微信登录后才能正常使用小程序功能',
							success(res) {
								console.log(0)
								console.log(res)
								//如果用户点击了确定按钮
								if (res.confirm) {
									uni.getUserProfile({
										desc: '获取你的昵称、头像、地区及性别',
										success: res => {
											console.log(res)
											let {
												userInfo
											} = res

											let req = {
												address: '',
												avatar: userInfo.avatarUrl,
												nickname: userInfo.nickName,
												sex: userInfo.gender,
												partnerNo
											}
											that.$api.userWxEdit(req).then(res => {
												console.log('userWxEdit--->', res)
												that.initUser();
											})
										},
										fail: res => {
											console.log(2);
											console.log(res)
											//拒绝授权
											uni.showToast({
												title: '您拒绝了请求,不能正常使用小程序',
												icon: 'error',
												duration: 2000,
												mask: true
											});
										}
									});
								} else if (res.cancel) {
									//如果用户点击了取消按钮
									console.log(3);
									uni.showToast({
										title: '您拒绝了请求,不能正常使用小程序',
										icon: 'error',
										duration: 2000,
										mask: true
									});
								}
							}
						});


					}else if(partnerNo){
						let req = {
							address: user.address,
							avatar: user.avatar,
							nickname: user.nickname,
							sex: user.sex,
							partnerNo
						}
						uni.showLoading({
							title: '加载中'
						});
						that.$api.userWxEdit(req).then(res => {
							console.log('userWxEdit--->', res.data.user)
							that.$store.commit('updataUser', res.data.user);
						})
					}
				})

			},
			//调用预览图片的方法
			ff_previewImage(myImgs, index = 0) {
				uni.previewImage({
					urls: [myImgs],
					current: index
				})
			},
			goPath(nowUrl, index) {
				// /static/images/code.jpg
				let url = index == 1 ? `${nowUrl}?referrerQrcode=${this.user.referrerQrcode}` : nowUrl;
				console.log('url--->', url)
				if(nowUrl=='/package/profile/index'){
					uni.setStorageSync('isProfile',true);
				}
				uni.navigateTo({
					url
				})
			},
			initUserx() {
				return new Promise(ress => {
					uni.showLoading({
						title: '加载中',
						mask: true
					});
					this.$api.userWxInfo().then(res => {
						if (res.code == 200) {
							this.$store.commit('updataUser', res.data.user);
							ress(res.data.user);
						}
					});
				})

			},
			initUser() {
				uni.showLoading({
					title: '加载中'
				});
				this.$api.userWxInfo().then(res => {
					if (res.code == 200) {
						this.user = res.data.user;
						this.$store.commit('updataUser', this.user);
						console.log('this.user--->', this.user);
						let nowformArr = JSON.parse(JSON.stringify(this.formArr))
						if (!this.user) {
							return
						}
						nowformArr.forEach(item => {

							if (item.text == '待支付') {
								item.num = this.user.unPayNum;
							} else if (item.text == '待发货') {
								item.num = this.user.unDeliveryNum;
							} else if (item.text == '待收货') {
								item.num = this.user.unReceiptNum;
							}
							// else if(item.text == '待评价'){
							// 	item.num = this.user.unCommentNum;
							// }
							else if (item.text == '售后') {
								item.num = this.user.serverNum;
							}

						});
						this.formArr = JSON.parse(JSON.stringify(nowformArr))
					}
				});
			},
		}
	}
</script>

<style scoped lang="scss">
	.myUser {
		width: 100%;
		height: 100vh;
		padding: 0 32rpx;
		box-sizing: border-box;
		background-color: #F7F8FC;

		.myUser__walletBottomMainItemBottomTxtx {
			height: 34rpx;
			font-size: 24rpx;
			font-family: 蘋方-簡;
			font-weight: bold;
			line-height: 34rpx;
			color: #2B2B33;
			opacity: 1;
		}

		.myUser__wallet {
			width: 686rpx;
			height: 222rpx;
			background: linear-gradient(180deg, #FFFFFF 0%, #FFFFFF 100%);
			box-shadow: 0px 0px 40rpx rgba(228, 228, 245, 0.4);
			opacity: 1;
			border-radius: 40rpx;
			box-sizing: border-box;
			padding: 32rpx;
			margin: 36rpx 0 20rpx;
			z-index: 99;
		}

		.myUser__walletff {
			width: 686rpx;
			background: linear-gradient(180deg, #FFFFFF 0%, #FFFFFF 100%);
			box-shadow: 0px 0px 40rpx rgba(228, 228, 245, 0.4);
			opacity: 1;
			border-radius: 40rpx;
			box-sizing: border-box;
			padding: 32rpx;
			margin-top: 20rpx;
		}

		.dd {
			height: 258rpx;
			margin: 20rpx 0;
		}

		.myUser__walletBottomMain {
			display: flex;
			justify-content: space-between;
		}

		.myUser__walletBottomMainItem {
			display: flex;
			flex-direction: column;
			align-items: center;
			flex: 1;
		}

		.myUser__walletBottomMainItemTop {
			position: relative;
			width: 80rpx;
			height: 80rpx;
		}

		.myUser__walletBottomMainItemTopImg {
			width: 80rpx;
			height: 80rpx;
			margin: 0 auto;
		}

		.myUser__walletBottomMainItemTopNum {
			position: absolute;
			right: 0;
			top: 0;
			width: 36rpx;
			height: 36rpx;
			line-height: 36rpx;
			background: #f44b5b;
			border-radius: 50%;
			opacity: 1;
			text-align: center;
			color: #FFFFFF;
			font-size: 26rpx;
			font-family: 蘋方-簡;
		}

		.myUser__walletBottom {
			display: flex;
			justify-content: space-around;

		}

		.myUser__walletTopRightAll {
			height: 40rpx;
			font-size: 24rpx;
			font-family: 蘋方-簡;
			font-weight: normal;
			line-height: 40rpx;
			color: #7B7D8D;
		}

		.myUser__walletBottomItemProe {
			height: 48rpx;
			font-size: 40rpx;
			font-family: DIN;
			font-weight: 500;
			text-align: center;
			line-height: 48rpx;
			color: #000000;
			-webkit-text-stroke: 0.5 #2B2B33;
			text-stroke: 0.5 #2B2B33;
			margin-bottom: 4rpx;
		}

		.myUser__walletBottomItemText {
			height: 34rpx;
			font-size: 24rpx;
			font-family: 蘋方-簡;
			font-weight: normal;
			line-height: 34rpx;
			color: #868896;
			opacity: 1;
			text-align: center;
		}

		.myUser__walletTop {
			display: flex;
			justify-content: space-between;
			margin-bottom: 32rpx;

		}

		.marTop {
			margin-bottom: 10rpx;
		}

		.myUser__walletBottomBigMainItem {

			height: 84rpx;
			display: flex;
			align-items: center;

		}

		.myUser__walletBottomBigMainItemTopImg {
			flex: 0 0 56rpx;
			width: 56rpx;
			height: 56rpx;
			margin-right: 16rpx;
		}

		.myUser__walletBottomBigMainItemTopNum {
			height: 36rpx;
			font-size: 26rpx;
			font-family: 蘋方-簡;
			font-weight: bold;
			line-height: 36rpx;
			color: #2B2B33;
			flex: 1;
		}

		.myUser__messageRightBoxImg {
			flex: 0 0 36rpx;
			width: 36rpx;
			height: 36rpx;
		}

		.myUser__messageLeftssBox {
			font-size: 28rpx;
			font-family: 蘋方-簡;
			font-weight: normal;
			color: #7B7D8D;
		}

		.myUser__walletTopMeText {
			height: 40rpx;
			font-size: 28rpx;
			font-family: 蘋方-簡;
			font-weight: normal;
			line-height: 40rpx;
			color: #7B7D8D;
		}

		.myUser__walletTopRight {
			display: flex;
			align-items: center;
		}


		.myUser_MMain {
			position: absolute;
			top: 0;
			left: 0;
			z-index: 9;
			padding: 0 32rpx;
			width: 100%;
			height: 100vh;
			padding: 0 32rpx;
			box-sizing: border-box;

		}

		.myUser_bg {
			width: 100%;
			position: absolute;
			top: 0;
			left: 0;
			z-index: 1;
		}

		.myUser__message {
			display: flex;
			width: 686rpx;
			background-color: transparent;

			&::after {

				border: none;

			}
		}

		.myUser__messageAdmin {
			flex: 0 0 120rpx;
			width: 120rpx;
			height: 120rpx;
			border-radius: 50%;
			border: 4rpx solid #deddff;
			box-sizing: border-box;
			margin-right: 20rpx;
		}

		.myUser__messageRight {
			flex: 1;
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;

		}

		.myUser__messageRightBox {
			display: flex;

			p {
				height: 56rpx;
				font-size: 40rpx;
				font-family: 蘋方-簡;
				font-weight: normal;
				line-height: 56rpx;
				color: #2B2B33;
				-webkit-text-stroke: 0.4000000059604645 #2B2B33;
				text-stroke: 0.4000000059604645 #2B2B33;
				opacity: 1;
				flex: 1;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				text-align: left;
			}

			display: flex;
			align-items: center;
		}

		.myUser__messageRightBoxImg {
			flex: 0 0 36rpx;
			width: 36rpx;
			height: 36rpx;
			margin-left: 10rpx;
		}

		.myUser__messageRightBoxTitle {
			height: 38rpx;
		}

	}
</style>
